package com.cuiweiyou.pipixiaautomator;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView listView;
    private View permissionLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarUtil.setWindowStatusBarColor(this, R.color.color_status_bar);

        List<String> devices = new ArrayList<>();
        devices.add("小米，MIUI9.5，Android7.1.1，皮皮虾V1.6.8");

        PipixiaListAdapter adapter = new PipixiaListAdapter(devices);

        listView = findViewById(R.id.listView);
        listView.setAdapter(adapter);

        permissionLayout = findViewById(R.id.permissionLayout);

        findViewById(R.id.permissionButton).setOnClickListener(this);
        findViewById(R.id.aboutTextView).setOnClickListener(this);
        findViewById(R.id.startButton).setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean on = isAccessibilitySettingsOn(this, PiixaAcsblityService.class);
        if (on) {
            permissionLayout.setVisibility(View.GONE);
        } else {
            permissionLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.aboutTextView: {
                startActivity(new Intent(this, AboutActivity.class));
                break;
            }
            case R.id.permissionButton: {
                startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
                break;
            }
            case R.id.startButton: {
                boolean on = isAccessibilitySettingsOn(this, PiixaAcsblityService.class);
                if (on) {
                    AppManager.startPipixia(getApplicationContext(), "com.sup.android.superb");
                } else {
                    Toast.makeText(this, "请首先开启权限", Toast.LENGTH_SHORT).show();
                }
                break;
            }
        }
    }

    private boolean isAccessibilitySettingsOn(Context context, Class acsbltyServiceClazz) {
        int accessibilityEnabled = 0;
        String serviceName = context.getPackageName() + "/" + acsbltyServiceClazz.getCanonicalName();
        ContentResolver contentResolver = context.getApplicationContext().getContentResolver();
        try {
            accessibilityEnabled = Settings.Secure.getInt(contentResolver, Settings.Secure.ACCESSIBILITY_ENABLED);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        TextUtils.SimpleStringSplitter stringSplitter = new TextUtils.SimpleStringSplitter(':');

        if (accessibilityEnabled == 1) {
            String settingValue = Settings.Secure.getString(contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
            if (settingValue != null) {
                stringSplitter.setString(settingValue);
                while (stringSplitter.hasNext()) {
                    String accessibilityService = stringSplitter.next();
                    if (accessibilityService.equalsIgnoreCase(serviceName)) {
                        return true;
                    }
                }
            }
        }

        return false;
    }
}
